package com.example.producer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Topic;

@Component
public class TopicProducer {


    private Topic testTopic;


    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    public void setTestTopic(Topic testTopic) {
        this.testTopic = testTopic;
    }

    @Autowired
    public void setJmsMessagingTemplate(JmsMessagingTemplate jmsMessagingTemplate) {
        this.jmsMessagingTemplate = jmsMessagingTemplate;
    }

    public void send(String message){
        try {
            System.out.println("往"+ testTopic.getTopicName() + "中发送消息");
        } catch (JMSException e) {
            e.printStackTrace();
        }
        jmsMessagingTemplate.convertAndSend(testTopic, message);
    }

    // 定时自动发送
    @Scheduled(fixedDelay = 100)
    public void autoSend(){
        send("这是topic消息");
    }
}
